//
//  subscribeViewController.m
//  Discover
//
//  Created by GreatranLee on 27/10/14.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "subscribeViewController.h"
#import "CollectionCell.h"
#import "subscribeCell.h"
@interface subscribeViewController ()<UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) NSMutableArray *titleArray1;
@property (nonatomic, retain) NSMutableArray *imgArray1;

@end

@implementation subscribeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.newsarray = [NSMutableArray array];
        self.imgArr = [NSMutableArray array];
        self.modelArray = [NSMutableArray array];
        self.imgArray1 = [NSMutableArray array];
        self.titleArray1 = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startConnection];
    [self setupViews];

}
- (void)setupViews
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.0 green:0.7 blue:1.0 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0 green:0.7 blue:1.0 alpha:1];
    flowLayout.itemSize = CGSizeMake(106, 106);
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.minimumLineSpacing = 1;
    flowLayout.sectionInset = UIEdgeInsetsMake(200, 0, 0, 0);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 568) collectionViewLayout:flowLayout];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[subscribeCell class] forCellWithReuseIdentifier:@"str"];
    [self.view addSubview:self.collection];
    [self.collection release];

    self.autoView = [[AutoScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
    //self.autoView.timeInterval = 2;
    [self.collection addSubview:self.autoView];
    [self.autoView release];

}
- (void)startConnection
{
//NSLog(@"%@", NSHomeDirectory());
    NSMutableArray *titleArr = [NSMutableArray array];
    [connection networkWithURL:@"http://iphone.myzaker.com/zaker/follow_promote.php?_appid=iphone&_dev=iPhone%2C8.1&_udid=5E54CD51-D5CC-4029-98DD-96B7ED27FB3F&_uid=&_version=4.51&m" completion:^(id result) {
        for (NSDictionary *dic in [[result objectForKey:@"data"] objectForKey:@"list"]) {
            MainViewCellModel *model = [[MainViewCellModel alloc]init];
            model.title = [dic objectForKey:@"title"];
            model.image = [dic objectForKey:@"promotion_img"];
            [titleArr addObject:model.title];
            [self.imgArr addObject:model.image];
//            NSLog(@"%@", self.imgArr);
//            NSLog(@"%@", model.image);
            [self.newsarray addObject:model];
            [self.collection reloadData];
            [self.autoView setImageUrls:self.imgArr];
            self.autoView.array = titleArr;            
        }
        NSArray *pathrray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [pathrray objectAtIndex:0];
        NSString *filepath = [path stringByAppendingPathComponent:@"rootBlocks.plist"];
        NSDictionary *rootArray = [NSDictionary dictionaryWithContentsOfFile:filepath];
        
        for (NSDictionary *dic in [rootArray objectForKey:@"blocksData"]) {
            [self.titleArray1 addObject:[dic objectForKey:@"block_title"]];
//            NSLog(@"%@", self.titleArray1);
            [self.imgArray1 addObject:[dic objectForKey:@"pic"]];
            //            [dic objectForKey:@"api_url"];
        }
    }];
    [self loadView];
    [self.collection reloadData];
//    NSLog(@"%@", self.titleArray);

}

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{
//    NSLog(@"%@", self.titleArray1);
    return self.titleArray1.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    subscribeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"str" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.6 blue:0.5 alpha:1];
    cell.TitleLabel.text = [self.titleArray1 objectAtIndex:indexPath.item];
    [cell.imgView setImageWithURL:[NSURL URLWithString:[self.imgArray1 objectAtIndex:indexPath.item]]];
//    NSLog(@"------------%@",self.titleArray1);
//    cell.model.image = [self.imgArray1 objectAtIndex:indexPath.item];
    return cell;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
